package SocialNet;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.traversal.TraversalDescription;
import org.neo4j.graphdb.traversal.Traverser;
import org.neo4j.helpers.collection.IteratorUtil;
import org.neo4j.kernel.Traversal;

import java.util.Date;

public class Status {
    private Node sNode;
    static final String TEXT = "TEXT";
    static final String DATE = "DATE";
    static final String LIKE = "LIKE";

    private RelTypes reltype;
    private int vote;

    public Status(Node in) {
        this.sNode = in;
        this.vote = 0;
    }

    public Node getNode() {
        return sNode;
    }

    public Person getPerson() {
        return new Person(getPersonNode());
    }

    private Node getPersonNode() {
        TraversalDescription traDes = Traversal.description().depthFirst().
                relationships(reltype.NEXT, Direction.INCOMING).
                relationships(reltype.STATUS, Direction.INCOMING).
                filter( Traversal.returnWhereLastRelationshipTypeIs(reltype.STATUS));
        Traverser traverser = traDes.traverse(getNode());
        return IteratorUtil.singleOrNull(traverser.iterator()).endNode();
    }

    public String getStatusText() {
        return (String)sNode.getProperty(TEXT);
    }

    public Date getDate() {
        Long l = (Long)sNode.getProperty(DATE);
        return new Date(l) ;
    }

    public Integer getLike() {
        return (Integer)sNode.getProperty(LIKE);
    }

    public void like() {
        this.sNode.setProperty(LIKE, ++vote);
    }
    
}

